﻿Public Class ProductoVenta2 
    'Public equivalencia As Decimal
    Public idventa As Integer
    Public idd As Integer
    Public costo As Decimal = 0.0
    Private Sub ProductosBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.Validate()
        Me.ProductosBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.DbFireAndIceDataSet)

    End Sub

    Private Sub ProductoVenta2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'DbFireAndIceDataSet.unidadescategoria' table. You can move, or remove it, as needed.
        Me.UnidadescategoriaTableAdapter.Fill(Me.DbFireAndIceDataSet.unidadescategoria)
        'TODO: This line of code loads data into the 'DbFireAndIceDataSet.unidadescategoria' table. You can move, or remove it, as needed.
        Me.UnidadescategoriaTableAdapter.Fill(Me.DbFireAndIceDataSet.unidadescategoria)
        Try
            'TODO: This line of code loads data into the 'DbFireAndIceDataSet.itemventadetalle' table. You can move, or remove it, as needed.
            Me.ItemventadetalleTableAdapter.Fill(Me.DbFireAndIceDataSet.itemventadetalle)
            'TODO: This line of code loads data into the 'DbFireAndIceDataSet.itemventadetalle' table. You can move, or remove it, as needed.
            Me.ItemventadetalleTableAdapter.Fill(Me.DbFireAndIceDataSet.itemventadetalle)
            'TODO: This line of code loads data into the 'DbFireAndIceDataSet.unidadesdemedida' table. You can move, or remove it, as needed.
            Me.UnidadesdemedidaTableAdapter.Fill(Me.DbFireAndIceDataSet.unidadesdemedida)
            'TODO: This line of code loads data into the 'DbFireAndIceDataSet.itemventa' table. You can move, or remove it, as needed.
            Me.ItemventaTableAdapter.Fill(Me.DbFireAndIceDataSet.itemventa)
            'TODO: This line of code loads data into the 'DbFireAndIceDataSet.productos' table. You can move, or remove it, as needed.
            Me.ProductosTableAdapter.Fill(Me.DbFireAndIceDataSet.productos)
        Catch ex As Exception
            AlertControl1.AppearanceCaption.ForeColor = Color.Red
            AlertControl1.Show(Owner, "Error", "No se pudieron cargar los datos")
        End Try
        RadioButton1.Checked = True
        idventa = My.Forms.ProductoVenta.IditemventaTextEdit.EditValue
        'MsgBox(idventa)

        ErrorProvider1.SetError(IdunidadLookUpEdit, Nothing)

        ErrorProvider1.SetError(TextEdit2, Nothing)
        ErrorProvider1.SetError(IdproductoLookUpEdit, Nothing)
    End Sub

    Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
        If RadioButton1.Checked = True Then
            RadioButton1.Checked = True
            RadioButton2.Checked = False
            IdproductoLookUpEdit.Visible = True
            VentaLookUpEdit.Visible = False
            Panel1.Enabled = True
        End If
    End Sub

    Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
        If RadioButton2.Checked = True Then
            RadioButton2.Checked = True
            RadioButton1.Checked = False
            IdproductoLookUpEdit.Visible = False
            VentaLookUpEdit.Visible = True
            Panel1.Enabled = False
        End If
    End Sub

    Private Sub SimpleButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton1.Click

        If RadioButton1.Checked = True Then
            Dim exisact As Decimal = 0
            idd = IdproductoLookUpEdit.GetColumnValue("idproducto")
            Dim cantidad As Integer = 0.0


            Dim totall As Decimal = 0.0


            cantidad = TextEdit1.EditValue

            totall = cantidad * costo
            If IdproductoLookUpEdit.Text = "" Then
                ErrorProvider1.SetError(IdproductoLookUpEdit, "Seleccione un Producto para agregar")
            Else
                ErrorProvider1.SetError(IdproductoLookUpEdit, Nothing)
               

                    If TextEdit2.EditValue < 1 Then
                        ErrorProvider1.SetError(TextEdit2, "Debe ingresar una cantidad de existencia")
                    Else
                        ErrorProvider1.SetError(TextEdit2, Nothing)
                        Try
                            ItemventadetalleTableAdapter.InsertQueryItemdetalle(idventa, idd, cantidad, costo, totall)
                            Me.Close()
                        Catch ex As Exception
                            AlertControl1.AppearanceCaption.ForeColor = Color.Red
                            AlertControl1.Show(Owner, "ERROR", "No se logro almacenar el Producto Detalle")
                        End Try

                    End If

                End If

            End If

        If RadioButton2.Checked = True Then
            Dim iditem As Integer
            Dim idpro As Double
            Dim canti As Double

            Dim costu As Double
            Dim tota As Double

            For i As Integer = 0 To GridView1.DataRowCount - 1

                'ItemventadetalleTableAdapter.FillQueryItemdetalle(DbFireAndIceDataSet.itemventadetalle, Textventa.EditValue)


                iditem = GridView1.GetRowCellValue(i, "iditemventa")
                idpro = GridView1.GetRowCellValue(i, "idproducto")

                costu = GridView1.GetRowCellValue(i, "costounitario")
                tota = GridView1.GetRowCellValue(i, "totallinea")
                canti = GridView1.GetRowCellValue(i, "cantidad")
                'tota = costu * canti
                Try
                    ItemventadetalleTableAdapter.InsertQueryItemdetalle(idventa, idpro, canti, costu, tota)
                    Me.Close()
                    'ItemventadetalleTableAdapter.Fill(DbFireAndIceDataSet.itemventadetalle)
                Catch ex As Exception
                    AlertControl1.AppearanceCaption.ForeColor = Color.Red
                    AlertControl1.Show(Owner, "ERROR", "No se pudo insertar el encabezado")
                End Try
                ' ItemventadetalleTableAdapter.InsertQueryItemdetalle(Val(IditemventaTextEdit.Text))
            Next i

        End If

        TextEdit1.EditValue = 0.0
        TextEdit2.EditValue = 0.0

    End Sub

    Private Sub IdunidadLookUpEdit_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles IdunidadLookUpEdit.EditValueChanged
        If IdunidadLookUpEdit.GetColumnValue("equivalenciauref") Is DBNull.Value Then
            ' = 1
            TextEdit1.EditValue = TextEdit2.EditValue

        Else
            'equivalencia = IdunidadLookUpEdit.GetColumnValue("equivalenciauref")
            TextEdit1.EditValue = Val(TextEdit2.EditValue)
        End If

        TextEdit1.EditValue = Val(TextEdit2.EditValue)

    End Sub

    Private Sub TextEdit2_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextEdit2.EditValueChanged
        TextEdit1.EditValue = TextEdit2.EditValue
    End Sub

    
    Private Sub VentaLookUpEdit_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles VentaLookUpEdit.EditValueChanged
        Dim idv As Integer
        If VentaLookUpEdit.GetColumnValue("iditemventa") Is DBNull.Value Then
            idv = 0
        Else
            idv = VentaLookUpEdit.GetColumnValue("iditemventa")
            Try
                ItemventadetalleTableAdapter.FillQueryItemdetalle(DbFireAndIceDataSet.itemventadetalle, idv)

            Catch ex As Exception
                AlertControl1.AppearanceCaption.ForeColor = Color.Red
                AlertControl1.Show(Owner, "ERROR", "No se pudieron cargar los datos")
            End Try
        End If
    End Sub

    Private Sub IdproductoLookUpEdit_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles IdproductoLookUpEdit.EditValueChanged
        Dim idd As Integer
        If IdproductoLookUpEdit.GetColumnValue("idunidadescategoria") Is DBNull.Value Then
        Else
            Try
                'UnidadesdemedidaTableAdapter.FillByUnidades(DbFireAndIceDataSet.unidadesdemedida, IdproductoLookUpEdit.GetColumnValue("idunidadescategoria"))
                idd = IdproductoLookUpEdit.GetColumnValue("idunidadescategoria")
                LabelControl1.Text = "Cantidad a ingresar en"
                UnidadescategoriaTableAdapter.FillBycategoriaunidad(DbFireAndIceDataSet.unidadescategoria, idd)
                LabelControl3.Text = IdunidadescategoriaLookUpEdit.GetColumnValue("description")
            Catch ex As Exception
                AlertControl1.AppearanceCaption.ForeColor = Color.Red
                AlertControl1.Show(Owner, "ERROR", "No se pudieron cargar los datos")
            End Try
        End If
        If IdproductoLookUpEdit.GetColumnValue("preciocomprauni") Is DBNull.Value Then
        Else
            'MsgBox(IdproductoLookUpEdit.GetColumnValue("preciocomprauni"))
            costo = IdproductoLookUpEdit.GetColumnValue("preciocomprauni")
        End If
        
    End Sub

    Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint

    End Sub

    Private Sub IdunidadescategoriaLookUpEdit_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles IdunidadescategoriaLookUpEdit.EditValueChanged
        idd = IdproductoLookUpEdit.GetColumnValue("idunidadescategoria")
        IdunidadescategoriaLookUpEdit.EditValue = idd
        LabelControl3.Text = IdunidadescategoriaLookUpEdit.GetColumnValue("description")
    End Sub
End Class